import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.JFrame;


public class Main extends JFrame implements KeyListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4666999768529658362L;
	private Image image;
	private Graphics graphic;
	private Player player = new Player(100, 100, 90, 14, 10, 1);
	public CopyOnWriteArrayList<Ball> balls =
		new CopyOnWriteArrayList<Ball>();
	
	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(700, 550);
		setDefault();
		setResizable(false);
		setBackground(new Color(100, 100, 255));
		addKeyListener(this);
		setVisible(true);
	}
	
	public void paint(Graphics g) {
		image = createImage(getWidth(), getHeight());
		graphic = image.getGraphics();
		
		paintComponent(graphic);
		g.drawImage(image, 0, 0, null);
		repaint();
	}
	
	public void paintComponent(Graphics g) {
		updateInformationOnScreen(g);
		//check health of Player
		if(player.getHealth() == 0) {
			g.setColor(Color.RED);
			g.drawString("Game Over", 300, 240);
			g.drawString("Press ESC to play again.", 300, 250);
			balls.clear();
			return;
		}
		
		player.render(g, this);
		
		for(Ball ball : balls) {
			ball.render(g, this);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Main();

	}
	
	public void updateInformationOnScreen(Graphics g)
	{
		Point loctation = getLocation();
		setTitle("My First Game. Location on screen: "
					+ loctation.x + " - " + loctation.y);
		int x = 100;
		int y = 80;
		int i = 1;
		
		g.setColor(new Color(50,255, 200));
		g.drawString("Velocity of Player: " + player.getVx(), x, y);
		y += 12;
		g.drawString("Health: " + player.getHealth(), x, y);
		y += 12;
		g.drawString("Number of ball on screen: " + balls.size(), x, y);
		
		for(Ball ball : balls) {
			y += 12;
			g.drawString("Velocity of Ball " + (i++) + ": vx = " +
					ball.getVx() + ", vy = " + ball.getVy(), x, y);
		}
		
		y += 12;
		g.drawString("Width of panel: " + player.getWidth()
				+ " Max = 510", x, y);
	}
	
	
	/**
	 * default
	 * health = 16;
	 */
	public void setDefault() {
		setLocation(425, 120);
		int x, y, w, h;
		h = 14;
		w = 150;
		x = getWidth()/2 - w/2;
		y = getHeight() - 6 - h;
		player = new Player(x, y, w, h, 16, 2);
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_RIGHT:
			player.setLeft(false);
			player.setRight(true);
			break;
		case KeyEvent.VK_LEFT:
			player.setRight(false);
			player.setLeft(true);
			break;
		case KeyEvent.VK_ENTER:
			if((player.getHealth() > 0) && (balls.size() < 15)) {
				
					Ball ball = new Ball(player,
							getWidth()/2 - 5, 26 + 10,
							0, 1);
					balls.add(ball);
			}
			break;
		case KeyEvent.VK_SPACE:
			if(player.getWidth() < 510) {
				player.setWidth(player.getWidth() + 10);
			}
			else {
				player.setWidth(90);
			}
			break;
		default:
			break;
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			setDefault();
			balls.clear();
			break;
		case KeyEvent.VK_RIGHT:
			player.setRight(false);
			break;
		case KeyEvent.VK_LEFT:
			player.setLeft(false);
			break;
		case KeyEvent.VK_UP:
			if(player.getVx() < 8) {
				player.setVx(player.getVx() + 1);
			}
			break;
		case KeyEvent.VK_DOWN:
			if(player.getVx() > 1) {
				player.setVx(player.getVx() - 1);
			}
			break;
		case KeyEvent.VK_F:
			player.setWidth(690);
			break;
		default:
			break;
		}
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
